package uk.ac.glasgow.demosocs.impl.electionTypes;

import java.util.Set;
import java.util.HashSet;

import uk.ac.glasgow.demosocs.Candidate;
import uk.ac.glasgow.demosocs.impl.InvalidElectorateSystem;

/**
 * Factory for creating ElectoralSystem instances. This is a singleton.
 */
public final class ElectoralSystemFactory {
	private static ElectoralSystemFactory instance = new ElectoralSystemFactory();

	private ElectoralSystemFactory() {
	}

	public static ElectoralSystemFactory getInstance() {
		return instance;
	}

	public ElectoralSystem createElectoralSystem(String type, Set<Candidate> candidates) throws InvalidElectorateSystem {
		if (type.equals(SimplePlurality.getFactoryName())) {
			return new SimplePlurality(candidates);

		} 
		
		else if (type.equals(AlternativeVote.getFactoryName()))
			return new AlternativeVote(candidates);
		
		else if (type.equals(Referendum.getFactoryName()))
			return new Referendum(candidates);
		
		else
			throw new InvalidElectorateSystem("Failed creating electorate system");

	}

	public Set<String> knownElectoralSystems() {
		Set<String> result = new HashSet<String>();
		result.add(SimplePlurality.getFactoryName());
		result.add(AlternativeVote.getFactoryName());
		return result;
	}
}